package zeroj.web.view;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import zeroj.web.action.ActionHelper;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;

/**
 * 
 * @author LYL(lyl at bitcomm dot cn)
 * 
 * @since 1.0
 *
 * 2008-9-24
 */
public class UrlDirective implements TemplateDirectiveModel {

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] models,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		
		String actionPath = ((TemplateScalarModel)params.get("action")).getAsString();
		if (actionPath == null) {
            throw new TemplateModelException("This directive expected parameter action.");
        }
		String url = ActionHelper.getUrl(actionPath);
		Writer out = env.getOut();
		out.write(url);
	}

}
